/**
 * jims
 */
package com.jims.master.modules.drug.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.drug.entity.DrugStorageProfile;
import com.jims.master.modules.drug.service.DrugStorageProfileService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 药品库存上下限Controller
 * @author DT
 * @version 2017-03-28
 */
@Controller
@RequestMapping(value = "${adminPath}/drug/drugStorageProfile")
public class DrugStorageProfileController extends BaseController {

	@Autowired
	private DrugStorageProfileService drugStorageProfileService;
	
	@ModelAttribute
	public DrugStorageProfile get(@RequestParam(required=false) String id) {
		DrugStorageProfile entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = drugStorageProfileService.get(id);
		}
		if (entity == null){
			entity = new DrugStorageProfile();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(DrugStorageProfile drugStorageProfile, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DrugStorageProfile> page = drugStorageProfileService.findPage(new Page<DrugStorageProfile>(request, response), drugStorageProfile);
		model.addAttribute("page", page);
		return "modules/drug/drugStorageProfileIndex";
	}
	@RequiresPermissions("user")
	@RequestMapping(value = {"warning", ""})
	public String warningList(DrugStorageProfile drugStorageProfile, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DrugStorageProfile> page = drugStorageProfileService.findStorageWarning(new Page<DrugStorageProfile>(request, response), drugStorageProfile);
		model.addAttribute("page", page);
		return "modules/drug/drugStorageWarningIndex";
	}

    /*
    * ZhuQ
    * 获取消耗量list
    * */
    @RequiresPermissions("user")
    @RequestMapping(value = {"findMinMaxList", ""})
    public String findMinMaxList(DrugStorageProfile drugStorageProfile, HttpServletRequest request, HttpServletResponse response, Model model) {
        drugStorageProfile.updateOrgId();
        Page<DrugStorageProfile> page = drugStorageProfileService.findMinMaxList(new Page<DrugStorageProfile>(request, response), drugStorageProfile);
        model.addAttribute("page", page);
        return "modules/drug/drugProfileCreateIndex";
    }

    /*
    * ZhuQ
    * 获取消耗量list
    * */
    @RequiresPermissions("user")
    @RequestMapping(value = {"saveProfile", ""})
    public String createMinMax(@RequestParam("infoId") String infoId, HttpServletRequest request, HttpServletResponse response, Model model) {
        DrugStorageProfile drugStorageProfile=new DrugStorageProfile();
        drugStorageProfile.setInfoId(infoId);
        drugStorageProfile=drugStorageProfileService.findByInfoId(drugStorageProfile);
		drugStorageProfile.setStorageId(null!= UserUtils.getUser().getOrgDept()?UserUtils.getUser().getOrgDept().getId():"");
		model.addAttribute("drugStorageProfile", drugStorageProfile);
        return "modules/drug/drugProfileCreateForm";
    }

    @RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(DrugStorageProfile drugStorageProfile, Model model) {
		model.addAttribute("drugStorageProfile", drugStorageProfile);
		return "modules/drug/drugStorageProfileForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(DrugStorageProfile drugStorageProfile, Model model) {
	    StringData data=new StringData();
		int i=drugStorageProfileService.save(drugStorageProfile);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(DrugStorageProfile drugStorageProfile) {
	    StringData data=new StringData();
		int i=drugStorageProfileService.delete(drugStorageProfile);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}

}